import time
from unittest import TestCase

import pymysql
from selenium import webdriver
from selenium.webdriver.common.by import By

class TestLend(TestCase):
    def test_lend1(self):
        chromeDriver = webdriver.Chrome()
        chromeDriver.get("http://localhost:90")
        chromeDriver.maximize_window()
        chromeDriver.find_element(By.XPATH, "//input[@id='username']").send_keys("lisi")
        chromeDriver.find_element(By.XPATH, "//input[@id='password']").send_keys("123456")
        chromeDriver.find_element(By.ID, "login_btn").click()
        time.sleep(1)

        chromeDriver.find_element(By.XPATH,'//*[@id="leftbaraside"]/div[2]/nav/ul/li[4]/a').click()
        time.sleep(1)
        chromeDriver.find_element(By.XPATH,'//*[@id="leftbaraside"]/div[2]/nav/ul/li[4]/ul/li[2]/a').click()
        time.sleep(1)
        try:
            chromeDriver.find_element(By.XPATH, '/html/body/div/div/main/div/div/div/div/div[2]/div/h4')
            a=0
        except:
            a=chromeDriver.find_element(By.XPATH,'/html/body/div/div/main/div/div/div/div/div[2]/div[2]/div[2]/div/button[6]/span').text
            a=int(a.strip('条'))

        chromeDriver.find_element(By.XPATH,'//*[@id="leftbaraside"]/div[2]/nav/ul/li[3]/a').click()
        time.sleep(1)
        chromeDriver.find_element(By.XPATH,'//*[@id="leftbaraside"]/div[2]/nav/ul/li[3]/ul/li[2]/a').click()
        time.sleep(1)
        chromeDriver.find_element(By.XPATH,'/html/body/div/div/main/div/div/div/div/div[2]/div/form/div[1]/input').send_keys('10000')
        chromeDriver.find_element(By.XPATH,'/html/body/div/div/main/div/div/div/div/div[2]/div/form/div[3]/input').send_keys('10')
        chromeDriver.find_element(By.XPATH,'//*[@id="submit"]').click()
        time.sleep(1)
        chromeDriver.find_element(By.XPATH,'/html/body/div[2]/div[2]/div/div/div/div/div/div/div/div[4]/button[1]').click()
        time.sleep(3)

        chromeDriver.find_element(By.XPATH,'//*[@id="leftbaraside"]/div[2]/nav/ul/li[4]/a').click()
        time.sleep(1)
        chromeDriver.find_element(By.XPATH,'//*[@id="leftbaraside"]/div[2]/nav/ul/li[4]/ul/li[2]/a').click()
        time.sleep(1)
        newa=chromeDriver.find_element(By.XPATH,'/html/body/div/div/main/div/div/div/div/div[2]/div[2]/div[2]/div/button[6]/span').text
        newa=int(newa.strip('条'))
        self.assertEqual(a+1,newa)
    def test_lend2(self):
        connect = pymysql.connect(host='localhost', user='root', password='root', database='finance')
        cursor = connect.cursor()
        sql = "select username from user"
        cursor.execute(sql)
        x = cursor.fetchall()
        self.assertIn(('lisi',), x)
        sql = "select username,id from user"
        cursor.execute(sql)
        x = cursor.fetchall()
        for i in range(len(x)):
            if 'lisi' in x[i - 1]:
                userid = x[i - 1][1]
        sql = f"update bankcard set balance=10000 where userId={userid} and defaultl=1"
        cursor.execute(sql)
        connect.commit()
        sql = f"select balance from bankcard where userId={userid} and defaultl=1"
        cursor.execute(sql)
        x = cursor.fetchall()
        money = x[0][0]

        chromeDriver = webdriver.Chrome()
        chromeDriver.get("http://localhost:90")
        chromeDriver.maximize_window()
        chromeDriver.find_element(By.XPATH, "//input[@id='username']").send_keys("lisi")
        chromeDriver.find_element(By.XPATH, "//input[@id='password']").send_keys("123456")
        chromeDriver.find_element(By.ID, "login_btn").click()
        time.sleep(1)
        chromeDriver.find_element(By.XPATH,'//*[@id="leftbaraside"]/div[2]/nav/ul/li[3]/a').click()
        time.sleep(1)
        chromeDriver.find_element(By.XPATH,'//*[@id="leftbaraside"]/div[2]/nav/ul/li[3]/ul/li[2]/a').click()
        time.sleep(1)
        chromeDriver.find_element(By.XPATH,'/html/body/div/div/main/div/div/div/div/div[2]/div/form/div[1]/input').send_keys('10000')
        chromeDriver.find_element(By.XPATH,'/html/body/div/div/main/div/div/div/div/div[2]/div/form/div[3]/input').send_keys('10')
        chromeDriver.find_element(By.XPATH,'//*[@id="submit"]').click()
        time.sleep(1)
        chromeDriver.find_element(By.XPATH,'/html/body/div[2]/div[2]/div/div/div/div/div/div/div/div[4]/button[1]').click()
        time.sleep(3)

        adminDriver = webdriver.Chrome()
        adminDriver.get("http://localhost:90")
        adminDriver.maximize_window()
        adminDriver.find_element(By.XPATH, "//input[@id='username']").send_keys("admin")
        adminDriver.find_element(By.XPATH, "//input[@id='password']").send_keys("123456")
        adminDriver.find_element(By.ID, "login_btn").click()
        time.sleep(1)
        adminDriver.find_element(By.XPATH,'//*[@id="leftbaraside"]/div[2]/nav/ul/li[5]/a').click()
        time.sleep(1)
        adminDriver.find_element(By.XPATH,'//*[@id="leftbaraside"]/div[2]/nav/ul/li[5]/ul/li[1]/a').click()
        time.sleep(1)
        adminDriver.find_element(By.XPATH,'/html/body/div/div/main/div/div/div/div/div[2]/div/table/tbody/tr[1]/td[7]/button[1]').click()
        time.sleep(1)
        adminDriver.find_element(By.XPATH,'/html/body/div[2]/div[2]/div/div/div/div/div/div/div/div[4]/button[1]').click()
        time.sleep(1)

        connect = pymysql.connect(host='localhost', user='root', password='root', database='finance')
        cursor = connect.cursor()
        sql = "select username,id from user"
        cursor.execute(sql)
        x = cursor.fetchall()
        for i in range(len(x)):
            if 'lisi' in x[i - 1]:
                userid = x[i - 1][1]
        sql = f"update bankcard set balance=10000 where userId={userid} and defaultl=1"
        cursor.execute(sql)
        x = cursor.fetchall()
        new_money = x[0][0]
        self.assertEqual(money+10000,new_money)
    def test_lend3(self):
        chromeDriver = webdriver.Chrome()
        chromeDriver.get("http://localhost:90")
        chromeDriver.maximize_window()
        chromeDriver.find_element(By.XPATH, "//input[@id='username']").send_keys("lisi5")
        chromeDriver.find_element(By.XPATH, "//input[@id='password']").send_keys("123456")
        chromeDriver.find_element(By.ID, "login_btn").click()
        time.sleep(1)

        chromeDriver.find_element(By.XPATH,'//*[@id="leftbaraside"]/div[2]/nav/ul/li[3]/a').click()
        time.sleep(1)
        chromeDriver.find_element(By.XPATH,'//*[@id="leftbaraside"]/div[2]/nav/ul/li[3]/ul/li[2]/a').click()
        time.sleep(1)
        chromeDriver.find_element(By.XPATH,'/html/body/div/div/main/div/div/div/div/div[2]/div/form/div[1]/input').send_keys('10000')
        chromeDriver.find_element(By.XPATH,'/html/body/div/div/main/div/div/div/div/div[2]/div/form/div[3]/input').send_keys('10')
        chromeDriver.find_element(By.XPATH,'//*[@id="submit"]').click()
        time.sleep(1)
        chromeDriver.find_element(By.XPATH,'/html/body/div[2]/div[2]/div/div/div/div/div/div/div/div[4]/button[1]').click()
        time.sleep(3)
        x=chromeDriver.find_element(By.XPATH,'/html/body/div[2]/div[2]/div/div/div/div/div/div/div/div[3]/div/div').text
        self.assertEqual(x,'申请失败!,请先绑定银行卡')
    def test_lend4(self):
        chromeDriver = webdriver.Chrome()
        chromeDriver.get("http://localhost:90")
        chromeDriver.maximize_window()
        chromeDriver.find_element(By.XPATH, "//input[@id='username']").send_keys("lisi")
        chromeDriver.find_element(By.XPATH, "//input[@id='password']").send_keys("123456")
        chromeDriver.find_element(By.ID, "login_btn").click()
        time.sleep(1)

        chromeDriver.find_element(By.XPATH,'//*[@id="leftbaraside"]/div[2]/nav/ul/li[3]/a').click()
        time.sleep(1)
        chromeDriver.find_element(By.XPATH,'//*[@id="leftbaraside"]/div[2]/nav/ul/li[3]/ul/li[2]/a').click()
        time.sleep(1)
        chromeDriver.find_element(By.XPATH,'/html/body/div/div/main/div/div/div/div/div[2]/div/form/div[1]/input').send_keys('100000000000000000')
        chromeDriver.find_element(By.XPATH,'/html/body/div/div/main/div/div/div/div/div[2]/div/form/div[3]/input').send_keys('10')
        chromeDriver.find_element(By.XPATH,'//*[@id="submit"]').click()
        time.sleep(1)
        chromeDriver.find_element(By.XPATH,'/html/body/div[2]/div[2]/div/div/div/div/div/div/div/div[4]/button[1]').click()
        time.sleep(3)
        x=chromeDriver.find_element(By.XPATH,'/html/body/div[2]/div[2]/div/div/div/div/div/div/div/div[3]/div/div').text
        self.assertEqual(x,'申请失败')
    def test_lend5(self):
        chromeDriver = webdriver.Chrome()
        chromeDriver.get("http://localhost:90")
        chromeDriver.maximize_window()
        chromeDriver.find_element(By.XPATH, "//input[@id='username']").send_keys("lisi")
        chromeDriver.find_element(By.XPATH, "//input[@id='password']").send_keys("123456")
        chromeDriver.find_element(By.ID, "login_btn").click()
        time.sleep(1)

        chromeDriver.find_element(By.XPATH,'//*[@id="leftbaraside"]/div[2]/nav/ul/li[3]/a').click()
        time.sleep(1)
        chromeDriver.find_element(By.XPATH,'//*[@id="leftbaraside"]/div[2]/nav/ul/li[3]/ul/li[2]/a').click()
        time.sleep(1)
        chromeDriver.find_element(By.XPATH,'/html/body/div/div/main/div/div/div/div/div[2]/div/form/div[1]/input').send_keys('10')
        chromeDriver.find_element(By.XPATH,'/html/body/div/div/main/div/div/div/div/div[2]/div/form/div[3]/input').send_keys('10')
        chromeDriver.find_element(By.XPATH,'//*[@id="submit"]').click()
        time.sleep(1)
        chromeDriver.find_element(By.XPATH,'/html/body/div[2]/div[2]/div/div/div/div/div/div/div/div[4]/button[1]').click()
        time.sleep(0.5)
        x=chromeDriver.find_element(By.XPATH,'/html/body/div[2]/div[2]/div/div/div/div/div/div/div/div[3]/div/div').text
        self.assertEqual(x,'申请失败,申请额度必须是100的整数倍')
    def test_lend6(self):
        chromeDriver = webdriver.Chrome()
        chromeDriver.get("http://localhost:90")
        chromeDriver.maximize_window()
        chromeDriver.find_element(By.XPATH, "//input[@id='username']").send_keys("lisi")
        chromeDriver.find_element(By.XPATH, "//input[@id='password']").send_keys("123456")
        chromeDriver.find_element(By.ID, "login_btn").click()
        time.sleep(1)

        chromeDriver.find_element(By.XPATH,'//*[@id="leftbaraside"]/div[2]/nav/ul/li[3]/a').click()
        time.sleep(1)
        chromeDriver.find_element(By.XPATH,'//*[@id="leftbaraside"]/div[2]/nav/ul/li[3]/ul/li[2]/a').click()
        time.sleep(1)
        chromeDriver.find_element(By.XPATH,'/html/body/div/div/main/div/div/div/div/div[2]/div/form/div[1]/input').send_keys('10')
        chromeDriver.find_element(By.XPATH,'/html/body/div/div/main/div/div/div/div/div[2]/div/form/div[3]/input').send_keys('10000')
        chromeDriver.find_element(By.XPATH,'//*[@id="submit"]').click()
        time.sleep(1)
        x=chromeDriver.find_element(By.XPATH,'/html/body/div/div/main/div/div/div/div/div[2]/div/form/div[3]/span').text
        self.assertEqual(x,'输入期限不合法！')
    def test_lend7(self):
        chromeDriver = webdriver.Chrome()
        chromeDriver.get("http://localhost:90")
        chromeDriver.maximize_window()
        chromeDriver.find_element(By.XPATH, "//input[@id='username']").send_keys("lisi")
        chromeDriver.find_element(By.XPATH, "//input[@id='password']").send_keys("123456")
        chromeDriver.find_element(By.ID, "login_btn").click()
        time.sleep(1)

        chromeDriver.find_element(By.XPATH,'//*[@id="leftbaraside"]/div[2]/nav/ul/li[3]/a').click()
        time.sleep(1)
        chromeDriver.find_element(By.XPATH,'//*[@id="leftbaraside"]/div[2]/nav/ul/li[3]/ul/li[2]/a').click()
        time.sleep(1)
        chromeDriver.find_element(By.XPATH,'/html/body/div/div/main/div/div/div/div/div[2]/div/form/div[1]/input').send_keys('10000')
        chromeDriver.find_element(By.XPATH,'/html/body/div/div/main/div/div/div/div/div[2]/div/form/div[3]/input').send_keys('9999')
        chromeDriver.find_element(By.XPATH,'//*[@id="submit"]').click()
        time.sleep(1)
        chromeDriver.find_element(By.XPATH,'/html/body/div[2]/div[2]/div/div/div/div/div/div/div/div[4]/button[1]').click()
        time.sleep(0.5)
        x=chromeDriver.find_element(By.XPATH,'/html/body/div[2]/div[2]/div/div/div/div/div/div/div/div[3]/div/div').text
        self.assertEqual(x,'申请成功!请等待管理员审核')